/* rcc: The retarded C compiler, a simple compiler for C
 * Copyright (C) 2016 Dylan Katz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "libir/libir.h"
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

static void _Noreturn usage(bool fail)
{
	fprintf(stdout,
		"Usage: rcc [options] [files...]\n\n"
		"Options:\n"
		" -h --help           Display this information\n"
		" -V --version        Display the version of RCC\n"
		" -I --include-path   Add directory to include path\n"
		" -D --define         Define a preprocessor symbol\n");
	exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
}

static void _Noreturn version()
{
	fprintf(stdout,
		"RCC Version 0.0.0\n"
		"Copyright (C) 2016 Dylan Katz\n"
		"This is free software; see the source for copying conditions. "
		"There is NO warranty; not even for MERCHANTABILITY or FITNESS "
		"FOR A PARTICULAR PURPOSE.\n");
	exit(EXIT_SUCCESS);
}

static int perfile(char *fname)
{
	FILE* f = fopen(fname, "r");
	if (f == NULL) {
		fprintf(stderr, "rcc: %s could not be opened\n", fname);
		switch (errno) { /* TODO: Add more types of errors */
		case 2:
			fprintf(stderr, "file does not exist\n");
			break;
		default:
			fprintf(stderr, "unknown error\n");
			break;
		}
		return 1;
	}
	/* This is where all of the processing on the file will take place */
	fclose(f);
	return 0;
}

int main(int argc, char **argv)
{
	int ch, res = 0;

	static char shortopts[] = "hVI:D:";
	static struct option longopts[] = {
		{"help",         no_argument,       NULL, 'h'},
		{"version",      no_argument,       NULL, 'V'},
		{"include-path", required_argument, NULL, 'I'},
		{"define",       required_argument, NULL, 'D'},
		{NULL,           0,                 NULL,  0 }
	};

	while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1)
		switch (ch) {
		case 'h':
			usage(false);
			/* Not Reached */
		case 'V':
			version();
			/* Not Reached */
		case 'I':
			/* Add an include path to the preprocessor state */
			break;
		case 'D':
			/* Add a defined symbol to the preprocessor state */
			break;
		default:
			usage(true);
			/* Not Reached */
		}

	if (argc == optind) {
		fprintf(stderr, "rcc: no files supplied\n");
		exit(EXIT_FAILURE);
	} else {
		for (int i = optind; i < argc; ++i)
			res += perfile(argv[i]);
		if (res != 0)
			exit(EXIT_FAILURE);
	}

	greet();
	exit(EXIT_SUCCESS);
}
